
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>玩转Angular2(5)--自定义input表单控件 | Here. There.</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="被删">
    
    <meta name="description" content="因为项目原因又玩上了Angular2(v4.0+)，《玩转Angular2》系列用于探索一些灵活或者新的用法。本文简单介绍封装使用ngModol实现自定义表单控件的过程。">
    
    
    
    
    <link rel="alternate" href="/atom.xml" title="Here. There." type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
    
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?3d902de4a19cf2bf179534ffd2dd7b7f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/sun.png" alt="Here. There." title="Here. There."/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Here. There.">Here. There.</a></h1>
				<h2 class="blog-motto">Love ice cream. Love sunshine. Love life. Love the world. Love myself. Love you.</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">首页</a></li>
					
						<li><a href="https://github.com/godbasin/godbasin.github.io">所有文章</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/categories">分类</a></li>
					
						<li><a href="/about">关于我</a></li>
					
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2017/06/03/angular2-free-5-custom-input-component/" title="玩转Angular2(5)--自定义input表单控件" itemprop="url">玩转Angular2(5)--自定义input表单控件</a>
  </h1>
  <p class="article-author">By
    
      <a href="https://godbasin.github.io" title="被删">被删</a>
    </p>
  <p class="article-time">
    <time datetime="2017-06-03T01:57:03.000Z" itemprop="datePublished">2017-06-03</time>
    更新日期:<time datetime="2018-12-18T15:31:30.902Z" itemprop="dateModified">2018-12-18</time>
    
  </p>
</header>
	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#NgModel相关"><span class="toc-number">1.</span> <span class="toc-text">NgModel相关</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#NgModel"><span class="toc-number">1.1.</span> <span class="toc-text">NgModel</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#NgControl"><span class="toc-number">1.2.</span> <span class="toc-text">NgControl</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ControlValueAccessor"><span class="toc-number">1.3.</span> <span class="toc-text">ControlValueAccessor</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#DefaultValueAccessor"><span class="toc-number">1.4.</span> <span class="toc-text">DefaultValueAccessor</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#NG-VALUE-ACCESSOR"><span class="toc-number">1.5.</span> <span class="toc-text">NG_VALUE_ACCESSOR</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#时间选择控件"><span class="toc-number">2.</span> <span class="toc-text">时间选择控件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#datetimepicker"><span class="toc-number">2.1.</span> <span class="toc-text">datetimepicker</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建自定义input控件"><span class="toc-number">2.2.</span> <span class="toc-text">创建自定义input控件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建自定义时间选择控件"><span class="toc-number">2.3.</span> <span class="toc-text">创建自定义时间选择控件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#抽象出class继承"><span class="toc-number">2.4.</span> <span class="toc-text">抽象出class继承</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">3.</span> <span class="toc-text">结束语</span></a></li></ol>
		</div>
		
		<p>因为项目原因又玩上了Angular2(v4.0+)，《玩转Angular2》系列用于探索一些灵活或者新的用法。<br>本文简单介绍封装使用ngModol实现自定义表单控件的过程。<br><a id="more"></a></p>
<h2 id="NgModel相关"><a href="#NgModel相关" class="headerlink" title="NgModel相关"></a>NgModel相关</h2><hr>
<h3 id="NgModel"><a href="#NgModel" class="headerlink" title="NgModel"></a>NgModel</h3><p><code>NgModel</code>用于从作用域创建一个<code>FormControl</code>实例，并将它绑定到一个表单控制元素。</p>
<ul>
<li><code>[ngModel]</code>: 单向绑定，作用域变更将同步到UI木板</li>
<li><code>[(ngModel)]</code>: 双向绑定，UI模版的变更也将同步到作用域</li>
</ul>
<p><code>NgModel</code>继承自<code>NgControl</code>。</p>
<h3 id="NgControl"><a href="#NgControl" class="headerlink" title="NgControl"></a>NgControl</h3><p><code>NgControl</code>是所有控制指令继承的基础类。它将一个<code>FormControl</code>绑定到DOM元素。</p>
<p><code>FormControl</code>、<code>FormGroup</code>和<code>FormArray</code>，三者都用于angular表单的值和状态的跟踪，区别在于是一个控件、一组控件或者是它们的组合。</p>
<p><code>AbstractControl</code>是三个具体表单类的抽象基类。并为它们提供了一些共同的行为和属性，其中有些是可观察对象（<code>Observable</code>）。<br><code>FormControl</code>用于跟踪一个单独的表单控件的值和有效性状态。它对应于一个HTML表单控件，比如输入框和下拉框。<br><code>FormGroup</code>用于跟踪一组<code>AbstractControl</code>的实例的值和有效性状态。该组的属性中包含了它的子控件。组件中的顶级表单就是一个<code>FormGroup</code>。<br><code>FormArray</code>用于跟踪<code>AbstractControl</code>实例组成的有序数组的值和有效性状态。</p>
<h3 id="ControlValueAccessor"><a href="#ControlValueAccessor" class="headerlink" title="ControlValueAccessor"></a>ControlValueAccessor</h3><p><code>ControlValueAccessor</code>用于在控制和原生元素之间建立联系，它封装了赋值到一个表现为<code>input</code>元素的DOM元素。</p>
<p>简单说，就是angular中的input是带有<code>[(ngModel)]</code>这个属性的，而我们想要自己控制这个input的写入过程，使用<code>ControlValueAccessor</code>就可以做到。</p>
<p><code>ControlValueAccessor</code>提供以下接口：</p>
<ul>
<li><code>writeValue(obj: any) : void</code>: 写入值到元素</li>
<li><code>registerOnChange(fn: any) : void</code>: 设置当控件接收到<code>change</code>事件时触发的回调</li>
<li><code>registerOnTouched(fn: any) : void</code>: 设置当控件接收到<code>touch</code>事件时触发的回调</li>
<li><code>setDisabledState(isDisabled: boolean) : void</code>: 该函数将在控件状态或者<code>disabled</code>值变化，根据值来对元素启用或失效</li>
</ul>
<p><code>ControlValueAccessor</code>继承自<code>DefaultValueAccessor</code>。</p>
<h3 id="DefaultValueAccessor"><a href="#DefaultValueAccessor" class="headerlink" title="DefaultValueAccessor"></a>DefaultValueAccessor</h3><p><code>DefaultValueAccessor</code>提供值写入和监听变化的默认访问，像<code>NgModel</code>, <code>FormControlDirective</code>, 和<code>FormControlName</code>指令会使用。</p>
<p><code>DefaultValueAccessor</code>提供类包括：</p>
<ul>
<li><code>onChange : (_: any) =&gt; {}</code>: <code>change</code>事件变化监听</li>
<li><code>onTouched : () =&gt; {}</code>: <code>touch</code>事件变化监听</li>
</ul>
<p>以及<code>ControlValueAccessor</code>（上面）的接口。</p>
<h3 id="NG-VALUE-ACCESSOR"><a href="#NG-VALUE-ACCESSOR" class="headerlink" title="NG_VALUE_ACCESSOR"></a>NG_VALUE_ACCESSOR</h3><p><code>NG_VALUE_ACCESSOR</code>提供一个<code>ControlValueAccessor</code>供表单控制使用。</p>
<h2 id="时间选择控件"><a href="#时间选择控件" class="headerlink" title="时间选择控件"></a>时间选择控件</h2><hr>
<h3 id="datetimepicker"><a href="#datetimepicker" class="headerlink" title="datetimepicker"></a>datetimepicker</h3><p>这里我们主要使用一个Bootstrap和jQuery的日期时间选择器插件–<a href="http://www.bootcss.com/p/bootstrap-datetimepicker/" target="_blank" rel="external">bootstrap-datetimepick</a>。</p>
<p>先简单介绍一下，我们可以使用该插件方便地进行日期和时间选择，从最大的十年视图到最小的分钟选择都可以自行调整。<br>具体一些配置项大家可以到官网上查看，这里就不详细介绍了，后面代码用到的会简单进行说明。</p>
<p>首先我们需要下载代码，这里放在了<code>assets/plugins/datepicker</code>文件夹里面。</p>
<p>然后添加进我们的应用程序就可以使用了：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// boostrap.ts</span></div><div class="line"><span class="built_in">require</span>(<span class="string">'./assets/plugins/datepicker/bootstrap-datetimepicker.min.js'</span>);</div><div class="line"><span class="built_in">require</span>(<span class="string">'./assets/plugins/datepicker/bootstrap-datetimepicker.zh-CN.js'</span>);</div></pre></td></tr></table></figure>
<h3 id="创建自定义input控件"><a href="#创建自定义input控件" class="headerlink" title="创建自定义input控件"></a>创建自定义input控件</h3><p>我们想要封装后的组件跟原生的angular组件一样，像表现为input的自定义控件，我们想要使用<code>[(ngModel)]</code>来进行双向绑定，我们需要使用<code>ControlValueAccessor</code>来拓展。</p>
<p>而这里<code>ControlValueAccessor</code>只是一个接口，我们应用它，还需要获取一些可用的服务，这时候需要注入<code>NG_VALUE_ACCESSOR</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">const</span> CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = &#123;</div><div class="line">    <span class="attr">provide</span>: NG_VALUE_ACCESSOR,</div><div class="line">    <span class="attr">useExisting</span>: forwardRef(<span class="function"><span class="params">()</span> =&gt;</span> CustomInputComponent),</div><div class="line">    <span class="attr">multi</span>: <span class="literal">true</span></div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>这里简单讲讲几个概念：</p>
<ol>
<li>我们自定义了一个访问控制服务，该服务包装为<code>NG_VALUE_ACCESSOR</code>服务，主要用于控制<code>ControlValueAccessor</code>相关的访问。</li>
<li>我们需要将自定义input控件提供给<code>NG_VALUE_ACCESSOR</code>，以便通过自定义方式控制父组件的<code>[(ngModel)]</code>以及其他表单相关的访问。</li>
<li><code>forwardRef</code>用于将目前还未获取到的依赖关联起来，这里我们关联后面的自定义Input组件。</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line">@Component(&#123;</div><div class="line">    <span class="attr">selector</span>: <span class="string">'custom-input'</span>,</div><div class="line">    <span class="attr">template</span>: <span class="string">`&lt;input [(ngModel)]="value" class="form-control" (blur)="onBlur()" /&gt;`</span>,</div><div class="line">    <span class="attr">providers</span>: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR] <span class="comment">// 注入访问控制服务</span></div><div class="line">&#125;)</div><div class="line"><span class="comment">// 继承ControlValueAccessor接口</span></div><div class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomInputComponent</span> <span class="title">implements</span> <span class="title">ControlValueAccessor</span></span>&#123;</div><div class="line"></div><div class="line">    <span class="comment">// 内部model值</span></div><div class="line">    private innerValue: any = <span class="string">''</span>;</div><div class="line"></div><div class="line">    <span class="comment">// 定义ControlValueAccessor提供的事件回调</span></div><div class="line">    private onTouched: <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">void</span> = noop;</div><div class="line">    private onChange: <span class="function">(<span class="params">_: any</span>) =&gt;</span> <span class="keyword">void</span> = noop;</div><div class="line"></div><div class="line">    <span class="comment">// 获取值的访问</span></div><div class="line">    get value(): any &#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.innerValue;</div><div class="line">    &#125;;</div><div class="line"></div><div class="line">    <span class="comment">// 设置值，同时触发change回调</span></div><div class="line">    set value(v: any) &#123;</div><div class="line">        <span class="keyword">if</span> (v !== <span class="keyword">this</span>.innerValue) &#123;</div><div class="line">            <span class="keyword">this</span>.innerValue = v;</div><div class="line">            <span class="keyword">this</span>.onChange(v);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// 失焦时触发回调</span></div><div class="line">    onBlur() &#123;</div><div class="line">        <span class="keyword">this</span>.onTouched();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// 表单ControlValueAccessor接口</span></div><div class="line">    writeValue(value: any) &#123;</div><div class="line">        <span class="keyword">if</span> (value !== <span class="keyword">this</span>.innerValue) &#123;</div><div class="line">            <span class="keyword">this</span>.innerValue = value;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// 表单ControlValueAccessor接口</span></div><div class="line">    registerOnChange(fn: any) &#123;</div><div class="line">        <span class="keyword">this</span>.onChange = fn;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// 表单ControlValueAccessor接口</span></div><div class="line">    registerOnTouched(fn: any) &#123;</div><div class="line">        <span class="keyword">this</span>.onTouched = fn;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>具体的实现实例参考<a href="https://embed.plnkr.co/nqKUSPWb6w5QXr8a0wEu/?show=preview" target="_blank" rel="external">Angular2 + Connect custom component to ngModel</a>。</p>
<h3 id="创建自定义时间选择控件"><a href="#创建自定义时间选择控件" class="headerlink" title="创建自定义时间选择控件"></a>创建自定义时间选择控件</h3><p>像我们定义一个时间选择控件，一般需要对外提供一些配置：</p>
<ul>
<li>选择精度（或自定义视图范围）<ul>
<li>这里提供：分钟（默认）、小时和天</li>
</ul>
</li>
<li>可选日期范围</li>
<li>是否禁用</li>
<li>是否必填</li>
<li>…</li>
</ul>
<p>以及通常我们提供一个值变更的回调，像<code>(change)</code>这样的事件。<br>下面看看代码实现：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> &#123;Component, Input, AfterViewInit, ElementRef, EventEmitter, Output, forwardRef&#125; <span class="keyword">from</span> <span class="string">'@angular/core'</span>;</div><div class="line"><span class="keyword">import</span> &#123;ControlValueAccessor, NG_VALUE_ACCESSOR&#125; <span class="keyword">from</span> <span class="string">'@angular/forms'</span>;</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">const</span> CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = &#123;</div><div class="line">  <span class="attr">provide</span>: NG_VALUE_ACCESSOR,</div><div class="line">  <span class="attr">useExisting</span>: forwardRef(<span class="function"><span class="params">()</span> =&gt;</span> DateTimePickerComponent),</div><div class="line">  <span class="attr">multi</span>: <span class="literal">true</span></div><div class="line">&#125;;</div><div class="line"></div><div class="line">@Component(&#123;</div><div class="line">  <span class="attr">selector</span>: <span class="string">'date-time-picker'</span>,</div><div class="line">  <span class="attr">template</span>: <span class="string">`&lt;input type="text" class="form-control" [disabled]="disabled" [(ngModel)]="value" (blur)="onBlur()" /&gt;`</span>,</div><div class="line">  <span class="attr">providers</span>: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR],</div><div class="line">&#125;)</div><div class="line"><span class="comment">// ControlValueAccessor: A bridge between a control and a native element.</span></div><div class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">DateTimePickerComponent</span> <span class="title">implements</span> <span class="title">AfterViewInit</span>, <span class="title">ControlValueAccessor</span> </span>&#123;</div><div class="line">  @Input() accuracy: string;  <span class="comment">// 日期选择的精度，默认分钟，可选 (hour | day)</span></div><div class="line">  @Input() startDate: string; <span class="comment">// 可选最早日期</span></div><div class="line">  @Input() endDate: string; <span class="comment">// 可选最晚日期</span></div><div class="line">  @Input() maxView: string; <span class="comment">// 最大视图</span></div><div class="line">  @Input() disabled: boolean = <span class="literal">false</span>; <span class="comment">// 是否禁用</span></div><div class="line"></div><div class="line">  @Output() change = <span class="keyword">new</span> EventEmitter&lt;any&gt;(); <span class="comment">// input的change事件回调</span></div><div class="line"></div><div class="line">  private el; <span class="comment">// 控件元素</span></div><div class="line">  private model: any; <span class="comment">// 内部model值</span></div><div class="line"></div><div class="line">  <span class="comment">// 定义ControlValueAccessor提供的事件回调</span></div><div class="line">  private onChange: <span class="function">(<span class="params">_: any</span>) =&gt;</span> <span class="keyword">void</span>;</div><div class="line">  private onTouched: <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">void</span>;</div><div class="line"></div><div class="line">  <span class="keyword">constructor</span>(el: ElementRef) &#123;</div><div class="line">    <span class="keyword">this</span>.el = el;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">// Lifecycle hook that is called after a component's view has been fully initialized.</span></div><div class="line">  ngAfterViewInit() &#123;</div><div class="line">    <span class="comment">/*</span></div><div class="line">     source:http://www.bootcss.com/p/bootstrap-datetimepicker/</div><div class="line">     minView: default 2</div><div class="line">     maxView: default 4</div><div class="line">     0 or 'hour' for the hour view （小时视图）</div><div class="line">     1 or 'day' for the day view 0-24h （日视图）</div><div class="line">     2 or 'month' for month view (the default) （月视图）</div><div class="line">     3 or 'year' for the 12-month overview （年视图）</div><div class="line">     4 or 'decade' for the 10-year overview. Useful for date-of-birth datetimepickers. （十年视图）</div><div class="line">     */</div><div class="line">    <span class="keyword">let</span> format = <span class="string">'yyyy-mm-dd hh:ii:00'</span>; <span class="comment">// 默认选择分钟，故秒数为00，datetimepicker不支持秒数的选择</span></div><div class="line">    <span class="keyword">let</span> minView = <span class="number">0</span>; <span class="comment">// 默认最小视图为分钟</span></div><div class="line"></div><div class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.accuracy === <span class="string">'hour'</span>) &#123;</div><div class="line">      format = <span class="string">'yyyy-mm-dd hh:00'</span>; <span class="comment">// 设置为小时，分钟和秒数需为00</span></div><div class="line">      minView = <span class="number">1</span>; <span class="comment">// 最小视图为小时</span></div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">this</span>.accuracy === <span class="string">'day'</span>) &#123;</div><div class="line">      format = <span class="string">'yyyy-mm-dd 00:00:00'</span>; <span class="comment">// 设置为天</span></div><div class="line">      minView = <span class="number">2</span>; <span class="comment">// 最小视图为日期</span></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    $(<span class="keyword">this</span>.el.nativeElement).find(<span class="string">'input'</span>).datetimepicker(&#123;</div><div class="line">      <span class="attr">language</span>: <span class="string">'zh-CN'</span>,</div><div class="line">      <span class="attr">autoclose</span>: <span class="literal">true</span>, <span class="comment">// 选择日期后自动关闭</span></div><div class="line">      maxView: <span class="built_in">parseInt</span>(<span class="keyword">this</span>.maxView, <span class="number">10</span>) || <span class="number">4</span>, <span class="comment">// 选择的最大视图，4为十年视图</span></div><div class="line">      startDate: <span class="keyword">this</span>.startDate || <span class="string">''</span>, <span class="comment">// 最早可选日期，默认不限制</span></div><div class="line">      endDate: <span class="keyword">this</span>.endDate || <span class="string">''</span>, <span class="comment">// 最晚可选日期，默认不限制</span></div><div class="line">      format, <span class="comment">// 格式化</span></div><div class="line">      minView, <span class="comment">// 选择的最小视图</span></div><div class="line">    &#125;)</div><div class="line">      .on(<span class="string">'hide'</span>, ev =&gt; &#123; <span class="comment">// 这里需要注意，我们使用箭头函数() =&gt; &#123;&#125;，则不会更改this的指向</span></div><div class="line">        <span class="keyword">this</span>.value = $(ev.target).val(); <span class="comment">// 更新值</span></div><div class="line">        <span class="keyword">this</span>.change.emit(&#123;<span class="attr">value</span>: $(ev.target).val()&#125;); <span class="comment">// 触发回调</span></div><div class="line">      &#125;);</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">// 获取值的访问</span></div><div class="line">  get value(): any &#123;</div><div class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.model;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">// 设置值，同时触发change回调</span></div><div class="line">  set value(v: any) &#123;</div><div class="line">    <span class="keyword">if</span> (v !== <span class="keyword">this</span>.model) &#123;</div><div class="line">      <span class="keyword">this</span>.model = v;</div><div class="line">      <span class="keyword">this</span>.onChange(v);</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">// 失焦时触发回调</span></div><div class="line">  onBlur() &#123;</div><div class="line">    <span class="keyword">this</span>.onTouched();</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">// 父组件的值变更时，更新model的值</span></div><div class="line">  writeValue(value: string): <span class="keyword">void</span>&#123;</div><div class="line">    <span class="keyword">if</span> (value !== <span class="keyword">this</span>.model) &#123;</div><div class="line">      <span class="keyword">this</span>.model = value;</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">// 表单ControlValueAccessor接口</span></div><div class="line">  registerOnChange(fn: <span class="function">(<span class="params">_: any</span>) =&gt;</span> &#123;&#125;): <span class="keyword">void</span> &#123;</div><div class="line">    <span class="keyword">this</span>.onChange = fn;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">// 表单ControlValueAccessor接口</span></div><div class="line">  registerOnTouched(fn: <span class="function"><span class="params">()</span> =&gt;</span> &#123;&#125;): <span class="keyword">void</span> &#123;</div><div class="line">    <span class="keyword">this</span>.onTouched = fn;</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="抽象出class继承"><a href="#抽象出class继承" class="headerlink" title="抽象出class继承"></a>抽象出class继承</h3><p>我们可以把相同的方法抽象出来，通过继承的方式，这样就能在多个相似组件通用了。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> &#123;forwardRef&#125; <span class="keyword">from</span> <span class="string">'@angular/core'</span>;</div><div class="line"><span class="keyword">import</span> &#123;ControlValueAccessor, NG_VALUE_ACCESSOR&#125; <span class="keyword">from</span> <span class="string">'@angular/forms'</span>;</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">customInputAccessor</span>(<span class="params">component</span>)</span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">        <span class="attr">provide</span>: NG_VALUE_ACCESSOR,</div><div class="line">        <span class="attr">useExisting</span>: forwardRef(<span class="function"><span class="params">()</span> =&gt;</span> component),</div><div class="line">        <span class="attr">multi</span>: <span class="literal">true</span></div><div class="line">    &#125;;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomInputComponent</span> <span class="title">implements</span> <span class="title">ControlValueAccessor</span> </span>&#123;</div><div class="line">    private model: any; <span class="comment">// 控件的值</span></div><div class="line">    private onChange: <span class="function">(<span class="params">_: any</span>) =&gt;</span> <span class="keyword">void</span>;</div><div class="line">    private onTouched: <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">void</span>;</div><div class="line"></div><div class="line">    <span class="comment">// get accessor</span></div><div class="line">    get value(): any &#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.model;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// set accessor including call the onchange callback</span></div><div class="line">    set value(v: any) &#123;</div><div class="line">        <span class="keyword">if</span> (v !== <span class="keyword">this</span>.model) &#123;</div><div class="line">            <span class="keyword">this</span>.model = v;</div><div class="line">            <span class="keyword">this</span>.onChange(v);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// Set touched on blur</span></div><div class="line">    onBlur() &#123;</div><div class="line">        <span class="keyword">this</span>.onTouched();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    writeValue(value: string): <span class="keyword">void</span>&#123;</div><div class="line">        <span class="keyword">if</span> (value !== <span class="keyword">this</span>.model) &#123;</div><div class="line">            <span class="keyword">this</span>.model = value;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// Set the function to be called when the control receives a change event.</span></div><div class="line">    registerOnChange(fn: <span class="function">(<span class="params">_: any</span>) =&gt;</span> &#123;&#125;): <span class="keyword">void</span> &#123;</div><div class="line">        <span class="keyword">this</span>.onChange = fn;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// registerOnTouched(fn: any) : void</span></div><div class="line">    registerOnTouched(fn: <span class="function"><span class="params">()</span> =&gt;</span> &#123;&#125;): <span class="keyword">void</span> &#123;</div><div class="line">        <span class="keyword">this</span>.onTouched = fn;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在datetimepick中继承：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> &#123;CustomInputComponent, customInputAccessor&#125; <span class="keyword">from</span> <span class="string">'../class/custom-input.class'</span>;</div><div class="line"></div><div class="line">@Component(&#123;</div><div class="line">  <span class="attr">selector</span>: <span class="string">'date-time-picker'</span>,</div><div class="line">  <span class="attr">template</span>: <span class="string">`&lt;input type="text" class="form-control" [disabled]="disabled" [(ngModel)]="value" (blur)="onBlur()" /&gt;`</span>,</div><div class="line">  <span class="attr">providers</span>: [customInputAccessor(DateTimePickerComponent)],</div><div class="line">&#125;)</div><div class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">DateTimePickerComponent</span> <span class="keyword">extends</span> <span class="title">CustomInputComponent</span> <span class="title">implements</span> <span class="title">AfterViewInit</span> </span>&#123;</div><div class="line">  @Input() accuracy: string;  <span class="comment">// 日期选择的精度，默认分钟，可选 (hour | day)</span></div><div class="line">  @Input() startDate: string; <span class="comment">// 可选最早日期</span></div><div class="line">  @Input() endDate: string; <span class="comment">// 可选最晚日期</span></div><div class="line">  @Input() maxView: string; <span class="comment">// 最大视图</span></div><div class="line">  @Input() disabled: boolean = <span class="literal">false</span>; <span class="comment">// 是否禁用</span></div><div class="line"></div><div class="line">  @Output() change = <span class="keyword">new</span> EventEmitter&lt;any&gt;(); <span class="comment">// input的change事件回调</span></div><div class="line"></div><div class="line">  private el; <span class="comment">// 控件元素</span></div><div class="line"></div><div class="line">  <span class="keyword">constructor</span>(el: ElementRef) &#123;</div><div class="line">    <span class="keyword">super</span>(); <span class="comment">// 继承</span></div><div class="line">    <span class="keyword">this</span>.el = el;</div><div class="line">  &#125;</div><div class="line">  ngAfterViewInit() &#123;</div><div class="line">    <span class="comment">// 原本的内容</span></div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>效果图：</p>
<p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1495970682%281%29.png" alt="image"></p>
<h2 id="结束语"><a href="#结束语" class="headerlink" title="结束语"></a>结束语</h2><hr>
<p>这节我们讲了自定义表单相关的一些概念，以及自定义一个时间选择input表单的实现过程。<br>很多时候我们都需要对不同的input自行封装，所以也可以单独抽象出来Class方便继承，又或者封装成指令等方式都是可以的。<br><a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/angular2-free/5-custom-input-component" target="_blank" rel="external">此处查看项目代码</a><br><a href="http://angular2-free.godbasin.com/5-custom-input-component/index.html#/home/page-setting" target="_blank" rel="external">此处查看页面效果</a></p>
  
		
		<p style="margin-top:50px;">查看Github有更多内容噢：<a href="https://github.com/godbasin">https://github.com/godbasin</a></p>
	</div>
	<img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" width="200" height="200" style="display:block;margin: 0 auto;" />
	<p style="text-align: center;margin-top: 10px;margin-bottom: 20px;">码生艰难，写文不易，给我家猪囤点猫粮了喵~</p>
	<div class="author-right">
  		<p>作者：被删</p>
  		<p>出处：<a href="https://godbasin.github.io">https://godbasin.github.io</a></p>
  		<p>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</p>
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/笔记/">笔记</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/angular2火锅/">angular2火锅</a>
</div>



<div class="article-share" id="share">

  
<!-- JiaThis Button BEGIN -->
<div class="jiathis_style_24x24">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_weixin"></a>
	<a class="jiathis_button_renren"></a>
	<a href="http://www.jiathis.com/share?uid=2134021" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
</div>
<script type="text/javascript">
var jiathis_config = {data_track_clickback:'true'};
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code_mini/jia.js?uid=2134021" charset="utf-8"></script>
<!-- JiaThis Button END -->   


</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2017/06/04/angular2-free-6-two-form-building/" title="玩转Angular2(6)--模型驱动和模板驱动的表单">
  <strong>PREVIOUS:</strong><br/>
  <span>
  玩转Angular2(6)--模型驱动和模板驱动的表单</span>
</a>
</div>


<div class="next">
<a href="/2017/06/02/angular2-free-4-create-sidebar/"  title="玩转Angular2(4)--制作左侧自动定位菜单">
 <strong>NEXT:</strong><br/> 
 <span>玩转Angular2(4)--制作左侧自动定位菜单
</span>
</a>
</div>

</nav>

	<!-- 如果不是首页且没使用 `comments: false` 关闭评论，则尝试加载评论 -->

    <!-- 配置中启用多说时，导入相应代码 -->
    


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
  <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#NgModel相关"><span class="toc-number">1.</span> <span class="toc-text">NgModel相关</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#NgModel"><span class="toc-number">1.1.</span> <span class="toc-text">NgModel</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#NgControl"><span class="toc-number">1.2.</span> <span class="toc-text">NgControl</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ControlValueAccessor"><span class="toc-number">1.3.</span> <span class="toc-text">ControlValueAccessor</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#DefaultValueAccessor"><span class="toc-number">1.4.</span> <span class="toc-text">DefaultValueAccessor</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#NG-VALUE-ACCESSOR"><span class="toc-number">1.5.</span> <span class="toc-text">NG_VALUE_ACCESSOR</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#时间选择控件"><span class="toc-number">2.</span> <span class="toc-text">时间选择控件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#datetimepicker"><span class="toc-number">2.1.</span> <span class="toc-text">datetimepicker</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建自定义input控件"><span class="toc-number">2.2.</span> <span class="toc-text">创建自定义input控件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建自定义时间选择控件"><span class="toc-number">2.3.</span> <span class="toc-text">创建自定义时间选择控件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#抽象出class继承"><span class="toc-number">2.4.</span> <span class="toc-text">抽象出class继承</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">3.</span> <span class="toc-text">结束语</span></a></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  	
	<div class="archiveslist">
		<p class="asidetitle">最近文章</p>
			<ul class="archive-list">
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/11/vue-for-everyone-2/" title="9102全员学Vue--2.怎么三两下拼出一个页面">9102全员学Vue--2.怎么三两...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/30/about-front-end-2-principle/" title="前端这几年--2.工作原则和选择">前端这几年--2.工作原则和选择...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/27/vue-for-everyone-1/" title="9102全员学Vue--1.如何理解前端和Vue">9102全员学Vue--1.如何理解...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/17/wxapp-latest-20190617/" title="小程序开发月刊第六期（20190617）">小程序开发月刊第六期（2019061...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/17/wxapp-generate/" title="小程序的诞生">小程序的诞生</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/05/15/wxapp-latest-20190515/" title="小程序开发月刊第五期（20190515）">小程序开发月刊第五期（2019051...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/04/15/wxapp-latest-20190415/" title="小程序开发月刊第四期（20190415）">小程序开发月刊第四期（2019041...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/04/13/about-front-end-1-begin-in/" title="前端这几年--1.转岗之路">前端这几年--1.转岗之路...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/03/15/wxapp-latest-20190315/" title="小程序开发月刊第三期（20190315）">小程序开发月刊第三期（2019031...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/03/10/work-2-article/" title="写文章这件事">写文章这件事</a>
				</li>
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
			</ul>
	</div>


  
  <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">归档</a></p>
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">五月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">三月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">二月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">十二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">十一月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">十月 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">九月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/08/">八月 2018</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">七月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">六月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">五月 2018</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">四月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">三月 2018</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">一月 2018</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">八月 2017</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">七月 2017</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">15</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">三月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">二月 2017</a><span class="archive-list-count">41</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/">一月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">十二月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">十一月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">十月 2016</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a><span class="archive-list-count">14</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a><span class="archive-list-count">9</span></li></ul>
  </div>


  
<div class="archiveslist">
	<p class="asidetitle"><a href="/categories">分类</a></p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/CSS炒饭/" title="CSS炒饭">CSS炒饭<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/D3小馒头/" title="D3小馒头">D3小馒头<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular2火锅/" title="angular2火锅">angular2火锅<sup>25</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular混搭/" title="angular混搭">angular混搭<sup>33</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/box2djs方糖/" title="box2djs方糖">box2djs方糖<sup>34</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/cyclejs哈根达斯/" title="cyclejs哈根达斯">cyclejs哈根达斯<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/jQuery杂烩/" title="jQuery杂烩">jQuery杂烩<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/js什锦/" title="js什锦">js什锦<sup>26</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/react沙拉/" title="react沙拉">react沙拉<sup>16</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/three-js奶茶/" title="three.js奶茶">three.js奶茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/vue八宝粥/" title="vue八宝粥">vue八宝粥<sup>28</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/webpack宾治/" title="webpack宾治">webpack宾治<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/web乱炖/" title="web乱炖">web乱炖<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/前端满汉全席/" title="前端满汉全席">前端满汉全席<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/喵/" title="喵">喵<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/小程序双皮奶/" title="小程序双皮奶">小程序双皮奶<sup>21</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/工作这杯茶/" title="工作这杯茶">工作这杯茶<sup>4</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/应用巧克力盒/" title="应用巧克力盒">应用巧克力盒<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/思想棉花糖/" title="思想棉花糖">思想棉花糖<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/柴米油盐工具集/" title="柴米油盐工具集">柴米油盐工具集<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/非前端钙片/" title="非前端钙片">非前端钙片<sup>7</sup></a>
			</li>
		
		</ul>
</div>


  
<div class="archiveslist">
	<p class="asidetitle">标签</p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/分享/" title="分享">分享<sup>44</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/原创部件/" title="原创部件">原创部件<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/心态/" title="心态">心态<sup>4</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/教程/" title="教程">教程<sup>65</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/晒猫/" title="晒猫">晒猫<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/笔记/" title="笔记">笔记<sup>121</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/算法/" title="算法">算法<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/自制插件/" title="自制插件">自制插件<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/逻辑实现/" title="逻辑实现">逻辑实现<sup>5</sup></a>
			</li>
		
		</ul>
</div>


    <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">about</a></p>
      <ul class="archive-list">
      	<li class="archive-list-item">
      		<a>wangbeishan@163.com</a>
      		<a href="https://github.com/godbasin">github.com/godbasin</a>
      	</li>
      </ul>
  </div>

  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<section class="info">
		<p> 即使梦想再小，却很伟大 ^_^ </p>
	</section>
	 
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2019 
		
		<a href="https://godbasin.github.io" target="_blank" title="被删">被删</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>







  </body>
</html>
